<template>
  <!-- 自定义导航 -->
  <custom-navbar />
  <scroll-view
    refresher-enabled="false"
    :refresher-triggered="isTriggered"
    @refresherrefresh="onRefreshrefresh"
    @scrolltolower="onScrolltolower"
    class="scroll-view"
    scroll-y
  >
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <template v-else>
      <!-- 自定义轮播图 -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 分类面板 -->
      <category-panel :list="categoryList"></category-panel>
      <!--热门推荐  -->
      <hot-panel :list="hotPanelList"></hot-panel>
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef"></XtxGuess>
      <view class="index">index1</view>
    </template>
  </scroll-view>
</template>

<script setup lang="ts">
import type { BannerItem, CategoryItem, PanelItem } from '@/types/home'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotPanelAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { XtxGuessInstance } from '@/types/component'
import { useGuessList } from '@/composables'
import PageSkeleton from './components/PageSkeleton.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import CustomNavbar from './components/CustomNavbar.vue'
import HotPanel from './components/HotPanel.vue'
// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}
// 获取前台分类数据
// const

// 传递一个 类型为CategoryItem对象的数组响应式变量 并且初始值为空数组
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
// 获取热门推荐数据
const hotPanelList = ref<PanelItem[]>([])
const getHomePanelData = async () => {
  const res = await getHomeHotPanelAPI()
  hotPanelList.value = res.result
}

const { guessRef, onScrolltolower } = useGuessList()
onScrolltolower()
// // 获取猜你喜欢组件实例
// const guessRef = ref<XtxGuessInstance>()

// // 滚动触底
// const onScrolltolower = () => {
//   // console.log('滚动触底啦')
//   guessRef.value?.getMore()
// }

const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefreshrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  // guessRef.value?.resetData()

  // 加载数据
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomePanelData(),
    guessRef.value?.getMore(),
  ])

  // 关闭动画
  isTriggered.value = false
}

// 获取猜你喜欢数据 (已经放到了XtxGuess的组件中去了)
// const GuessList = ref([])
// const getGuessData = async()=>{
//   const res =await getGuessAPI()
//   console.log(res);
// }

// 骨架屏是否出现
const isLoading = ref(false)
// 刚进去页面的时候去调用。

onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomePanelData()])
  isLoading.value = false
  // getGuessData()
})
</script>

<style>
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
